package ge.gen.eBdgt.action;

import ge.gen.eBdgt.graph.PlanToActualsGraph;
import ge.gen.eBdgt.service.PlanToActualsService;
import ge.gen.eBdgt.utils.Constants;
import ge.gen.eBdgt.vo.CRUD;
import ge.gen.eBdgt.vo.CmuTrackVO;
import ge.gen.eBdgt.vo.DescrVO;
import ge.gen.eBdgt.vo.IcnVO;
import ge.gen.eBdgt.vo.PlanToActualsVO;
import ge.gen.eBdgt.vo.ValueObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletContext;

import com.gp.api.jsp.MxChartDescription;
import com.gp.api.jsp.MxServerComponent;
import com.gp.api.styles.MxElementStyle;

@SuppressWarnings("serial")
public class IcnDetailAction extends BaseAction {
	protected String icn = null;
	protected String icn2nd = null;
	protected String customer = null;
	protected String des = null;
	protected String description = null;
	protected String ee = null;
	protected String genSn = null;
	protected String me = null;
	protected String modDwg = null;
	protected String orderDate = null;
	protected String outageDate = null;
	protected String pm = null;
	protected String reqEng = null;
	protected String requestDate = null;
	protected String station = null;
	protected String status = null;
	protected String timesNo = null;
	protected String orderNo = null;
	protected String endDate = null;
	protected String milstnDate = null;
	protected String planHrs = null;
	protected String ctgryTyp = null;
	protected String mainWorkScope1 = null;
	protected String mainWorkScope2 = null;
	protected String addlScope1 = null;
	protected String addlScope2 = null;
	protected String addlScope3 = null;
	protected String addlScope4 = null;
	protected String notes = null;
	protected String budget = null;

	protected int minExpectedUpdates = 0;
	public List<Choice> addlnList;

	public String[] selectedAddlnList = new String[4];
	public String selectedChoices;
	public String[] addlnArray = { "NAMEPLATE", "RET RING", "COLL RING",
			"CON RING", "EWBASKET", "HSB", "HVB", "P BARS", "TIL 965",
			"TIL 1002", "TIL 1145", "TIL 1292", "TIL 1515", "TIL 1516",
			"UPRATE", "DERATE" };

	public IcnDetailAction() throws Exception {

	}

	public String getIcn() {
		return icn;
	}

	public void setIcn(String icn) {
		this.icn = icn;
	}

	public String getIcn2nd() {
		return icn2nd;
	}

	public void setIcn2nd(String icn2nd) {
		this.icn2nd = icn2nd;
	}

	public String getCustomer() {
		return customer;
	}

	public void setCustomer(String customer) {
		this.customer = customer;
	}

	public String getDes() {
		return des;
	}

	public void setDes(String des) {
		this.des = des;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getEe() {
		return ee;
	}

	public void setEe(String ee) {
		this.ee = ee;
	}

	public String getGenSn() {
		return genSn;
	}

	public void setGenSn(String genSn) {
		this.genSn = genSn;
	}

	public String getMe() {
		return me;
	}

	public void setMe(String me) {
		this.me = me;
	}

	public String getModDwg() {
		return modDwg;
	}

	public void setModDwg(String modDwg) {
		this.modDwg = modDwg;
	}

	public String getOrderDate() {
		return orderDate;
	}

	public void setOrderDate(String orderDate) {
		this.orderDate = orderDate;
	}

	public String getOutageDate() {
		return outageDate;
	}

	public void setOutageDate(String outageDate) {
		this.outageDate = outageDate;
	}

	public String getPm() {
		return pm;
	}

	public void setPm(String pm) {
		this.pm = pm;
	}

	public String getReqEng() {
		return reqEng;
	}

	public void setReqEng(String reqEng) {
		this.reqEng = reqEng;
	}

	public String getRequestDate() {
		return requestDate;
	}

	public void setRequestDate(String requestDate) {
		this.requestDate = requestDate;
	}

	public String getStation() {
		return station;
	}

	public void setStation(String station) {
		this.station = station;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getTimesNo() {
		return timesNo;
	}

	public void setTimesNo(String timesNo) {
		this.timesNo = timesNo;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getMilstnDate() {
		return milstnDate;
	}

	public void setMilstnDate(String milstnDate) {
		this.milstnDate = milstnDate;
	}

	public String getPlanHrs() {
		return planHrs;
	}

	public void setPlanHrs(String planHrs) {
		this.planHrs = planHrs;
	}

	public String getCtgryTyp() {
		return ctgryTyp;
	}

	public void setCtgryTyp(String ctgryTyp) {
		this.ctgryTyp = ctgryTyp;
	}

	public String getBudget() {
		return budget;
	}

	public void setBudget(String budget) {
		this.budget = budget;
	}

	public int getMinExpectedUpdates() {
		return minExpectedUpdates;
	}

	public void setMinExpectedUpdates(int minExpectedUpdates) {
		this.minExpectedUpdates = minExpectedUpdates;
	}

	public List<DescrVO> getDescrList() {
		return descrList;
	}

	public void setDescrList(List<DescrVO> descrList) {
		this.descrList = descrList;
	}

	public List<DescrVO> getStatusList() {
		return statusList;
	}

	public void setStatusList(List<DescrVO> statusList) {
		this.statusList = statusList;
	}

	public List<DescrVO> getCtgryList() {
		return ctgryList;
	}

	public void setCtgryList(List<DescrVO> ctgryList) {
		this.ctgryList = ctgryList;
	}

	public String getSelectedChoices() {
		return selectedChoices;
	}

	public void setSelectedChoices(String selectedChoices) {
		this.selectedChoices = selectedChoices;
	}

	public String[] getSelectedAddlnList() {
		return selectedAddlnList;
	}

	public void setSelectedAddlnList(String[] selectedAddlnList) {
		this.selectedAddlnList = selectedAddlnList;
	}

	protected IcnVO detailIcnVO;

	protected void callSelectProxy() throws Exception {
		if (getIcnListMap() != null)
			detailIcnVO = getIcnListMap().get(icn);
		if (detailIcnVO == null)
			detailIcnVO = icnService.getIcnInfo(icn);
		if (detailIcnVO == null) {
			detailIcnVO = new IcnVO();
		} else {
			populateFormFromIcnVO();
			PlanToActualsService planToActuals = new PlanToActualsService();
			List<PlanToActualsVO> planToActualsVOList = planToActuals
					.getPlanToActualsVOList(detailIcnVO.getTimesNo());
			if (planToActualsVOList != null && planToActualsVOList.size() > 0)
				createChart(planToActualsVOList);
		}
		detailIcnVO.setExistingRow(true);
		request.getSession().setAttribute("detailIcnVO", detailIcnVO);
	}

	public String save() {
		try {
			detailIcnVO = (IcnVO) request.getSession().getAttribute(
					"detailIcnVO");
			populateIncVO();
			prepareVoForSave(detailIcnVO);
			if (minExpectedUpdates > 0) {
				if (addFlag == false)
					icnService.updateIcn(detailIcnVO);
				else
					icnService.insertIcn(detailIcnVO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		addActionMessage("Save Successfull !!!");
		return INPUT;
	}

	protected void populateIncVO() {
		if (detailIcnVO == null)
			detailIcnVO = new IcnVO();
		detailIcnVO.setIcn(icn);
		detailIcnVO.setIcn2nd(icn2nd);
		detailIcnVO.setCustomer(customer);
		detailIcnVO.setDes(des);
		detailIcnVO.setDescription(description);
		detailIcnVO.setEe(ee);
		detailIcnVO.setGenSn(genSn);
		detailIcnVO.setMe(me);
		detailIcnVO.setModDwg(modDwg);
		detailIcnVO.setOrderDate(orderDate);
		detailIcnVO.setOutageDate(outageDate);
		detailIcnVO.setPm(pm);
		detailIcnVO.setReqEng(reqEng);
		detailIcnVO.setRequestDate(requestDate);
		detailIcnVO.setStation(station);
		detailIcnVO.setStatus(status);
		detailIcnVO.setTimesNo(timesNo);
		detailIcnVO.setNotes(notes);
		detailIcnVO.setMainWorkScope1(mainWorkScope1);
		detailIcnVO.setMainWorkScope2(mainWorkScope2);
		initalize(selectedAddlnList);
		if (selectedChoices != null && selectedChoices.length() > 0) {
			String[] arr = selectedChoices.split(",");
			if (arr.length > 0) {
				detailIcnVO.setAddlScope1(arr[0]);
				selectedAddlnList[0] = getValue(arr[0]);
			}
			if (arr.length > 1) {
				detailIcnVO.setAddlScope2(arr[1]);
				selectedAddlnList[1] = getValue(arr[1]);
			}
			if (arr.length > 2) {
				detailIcnVO.setAddlScope3(arr[2]);
				selectedAddlnList[2] = getValue(arr[2]);
			}
			if (arr.length > 3) {
				detailIcnVO.setAddlScope4(arr[3]);
				selectedAddlnList[3] = getValue(arr[3]);
			}
		}
		detailIcnVO.setOrderInfo(getOrderInfoVO());
	}

	private void initalize(String[] selectedAddlnList2) {
		for (int i = 0; i < 4; i++) {
			selectedAddlnList[i] = "";
		}

	}

	private CmuTrackVO getOrderInfoVO() {
		CmuTrackVO vo = new CmuTrackVO();
		vo.setCtgryTyp(ctgryTyp);
		vo.setEndDate(endDate);
		vo.setMilstnDate(milstnDate);
		vo.setPlanHrs(planHrs);
		vo.setOrderNo(timesNo);
		vo.setBudget(budget);
		return vo;
	}

	protected void populateFormFromIcnVO() {
		icn = detailIcnVO.getIcn();
		icn2nd = detailIcnVO.getIcn2nd();
		customer = detailIcnVO.getCustomer();
		des = detailIcnVO.getDes();
		description = detailIcnVO.getDescription();
		ee = detailIcnVO.getEe();
		genSn = detailIcnVO.getGenSn();
		me = detailIcnVO.getMe();
		modDwg = detailIcnVO.getModDwg();
		orderDate = detailIcnVO.getOrderDate();
		outageDate = detailIcnVO.getOutageDate();
		pm = detailIcnVO.getPm();
		reqEng = detailIcnVO.getReqEng();
		requestDate = detailIcnVO.getRequestDate();
		station = detailIcnVO.getStation();
		status = detailIcnVO.getStatus();
		timesNo = detailIcnVO.getTimesNo();
		mainWorkScope1 = detailIcnVO.getMainWorkScope1();
		mainWorkScope2 = detailIcnVO.getMainWorkScope2();
		notes = detailIcnVO.getNotes();
		selectedAddlnList[0] = getValue(detailIcnVO.getAddlScope1());
		selectedAddlnList[1] = getValue(detailIcnVO.getAddlScope2());
		selectedAddlnList[2] = getValue(detailIcnVO.getAddlScope3());
		selectedAddlnList[3] = getValue(detailIcnVO.getAddlScope4());
		CmuTrackVO cmuTrackInfo = detailIcnVO.getOrderInfo();
		if (cmuTrackInfo != null) {
			orderNo = detailIcnVO.getTimesNo();
			endDate = cmuTrackInfo.getEndDate();
			milstnDate = cmuTrackInfo.getMilstnDate();
			planHrs = cmuTrackInfo.getPlanHrs();
			ctgryTyp = cmuTrackInfo.getCtgryTyp();
			budget = cmuTrackInfo.getBudget();
		}
	}

	private String getValue(String s) {
		if (s == null)
			return "";
		return s.trim();
	}

	protected ValueObject prepareVoForSave(final ValueObject vo) {
		if (vo.isModified()) {
			vo.setCRUD(CRUD.UPDATE);
		}
		if (vo.getCRUD().equals(CRUD.READ) == false) {
			minExpectedUpdates++;
		}
		return vo;
	}

	protected void prepareForSave() throws Exception {
		populateIncVO();
		prepareVoForSave(detailIcnVO);
	}

	public IcnVO getDetailIcnVO() {
		return detailIcnVO;
	}

	public void setDetailIcnVO(IcnVO detailIcnVO) {
		this.detailIcnVO = detailIcnVO;
	}

	public List<DescrVO> getReqEngrList() {
		return reqEngrList;
	}

	public void setReqEngrList(List<DescrVO> reqEngrList) {
		this.reqEngrList = reqEngrList;
	}

	public List<Choice> getAddlnList() {
		List<Choice> lst = new ArrayList();
		for (int i = 0; i < addlnArray.length; i++) {
			Choice c = new Choice(addlnArray[i], addlnArray[i]);
			lst.add(c);
		}
		return lst;
	}

	public void setAddlnList(List<Choice> addlnList) {
		this.addlnList = addlnList;
	}

	public String getMainWorkScope1() {
		return mainWorkScope1;
	}

	public void setMainWorkScope1(String mainWorkScope1) {
		this.mainWorkScope1 = mainWorkScope1;
	}

	public String getMainWorkScope2() {
		return mainWorkScope2;
	}

	public void setMainWorkScope2(String mainWorkScope2) {
		this.mainWorkScope2 = mainWorkScope2;
	}

	public String getAddlScope1() {
		return addlScope1;
	}

	public void setAddlScope1(String addlScope1) {
		this.addlScope1 = addlScope1;
	}

	public String getAddlScope2() {
		return addlScope2;
	}

	public void setAddlScope2(String addlScope2) {
		this.addlScope2 = addlScope2;
	}

	public String getAddlScope3() {
		return addlScope3;
	}

	public void setAddlScope3(String addlScope3) {
		this.addlScope3 = addlScope3;
	}

	public String getAddlScope4() {
		return addlScope4;
	}

	public void setAddlScope4(String addlScope4) {
		this.addlScope4 = addlScope4;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	protected void callSaveProxy() throws Exception {

	}

	public String select() throws Exception {
		addFlag = false;
		callSelectProxy();
		return SUCCESS;
	}

	protected String processSelectResults() {
		String retVal = SUCCESS;
		if (request.getAttribute("actionToFwdTo") != null) {
			request.setAttribute("tempActionToFwdTo", request
					.getAttribute("actionToFwdTo"));
			request.removeAttribute("actionToFwdTo");
			retVal = "tabActionForward";
		}
		return retVal;
	}

	public String execute() throws Exception {
		return select();
	}

	private void createGraphChart(PlanToActualsGraph graph) {
		ServletContext application = request.getSession().getServletContext();
		MxServerComponent svr = MxServerComponent
				.getDefaultInstance(application);
		MxChartDescription myChart = svr.newImageSpec();
		myChart.width = 1000;
		myChart.height = 1000;
		myChart.type = "PNG";
		myChart.style = graph.getStyle().toXML();
		myChart.model = graph.getModel().toXML();
		String imageSream = svr.getImageTag(myChart, "getimage.jsp?image=");
		request.getSession().setAttribute("imageSream", imageSream);
	}

	public void createChart(List<PlanToActualsVO> planToActualsVOList) {
		if (request != null) {
			PlanToActualsGraph graph = new PlanToActualsGraph(
					planToActualsVOList, MxElementStyle.Shape.LINE);
			createGraphChart(graph);
		}
	}

}

class Choice {
	public Choice(String key, String displayName) {
		this.key = key;
		this.displayName = displayName;
	}

	String key;
	String displayName;

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

}